#include "widget.h"
#include "ui_widget.h"

#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>

#include <qtextcodec.h>

#include <QDebug>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

//    tf_init();
//    if(yolo_init()!=1){
//        cout << "yolo init fail" <<endl;
//    }

    //1.1、初始化变量
    MainSerialPortRecvFrameNumber=0;
    MainSerialPortRecvErrorFrameNumber=0;
    MainSerialBaudRate = 115200;

    MainSerialPortOneFrameSize = 50;

    MainSerial = NULL;

    ChatStatu = FreeStatu;
    ContinIndex = 0;
    Precise_ContinIndex = 0;

    RepeatFlag = false;

    AutoRunIndex = 0;


    //2.1、特殊成分
    DelayTimer = new QTimer(this);
    OverTimer = new QTimer(this);
    connect(OverTimer, SIGNAL(timeout()), this, SLOT(OverTimer_Fun()));

    codec = QTextCodec::codecForName("UTF-8");

    //3、控件
    //3.1、串口部分
    RecvText = new QPlainTextEdit(codec->toUnicode("串口在关闭状态"));
    ComboBox = new QComboBox();
    SendText = new QLineEdit(tr("_&X13;Y5;S50;"));

    QPushButton *ScanButton = new QPushButton(codec->toUnicode("扫描端口"));
    connect(ScanButton,SIGNAL(clicked(bool)),this,SLOT(scanport_slot()));

    OpenButton = new QPushButton(codec->toUnicode("打开端口"));
    connect(OpenButton,SIGNAL(clicked(bool)),this,SLOT(openport_slot()) );

    QPushButton *SendButton = new QPushButton(codec->toUnicode("发送"));
    connect(SendButton,SIGNAL(clicked(bool)),this,SLOT(MainSerialSendMsgEvent()) );

    //3.1.2 serial delay timer
    SerialDelayTimer = new QTimer(this);

    //3.2、坐标参数显示部分
    ArmValLab_label.push_back(new QLabel(tr("x")));
    ArmValLab_label.push_back(new QLabel(tr("y")));
    ArmValLab_label.push_back(new QLabel(tr("prime")));
    ArmValLab_label.push_back(new QLabel(tr("vice")));

    ArmVal_label.push_back(new QLabel(tr("0")));
    ArmVal_label.push_back(new QLabel(tr("0")));
    ArmVal_label.push_back(new QLabel(tr("0")));
    ArmVal_label.push_back(new QLabel(tr("0")));

    //3.3、滚动区域
    scrollarea = new QScrollArea();
    //scrollarea->setGeometry(3, 3, 500, 1000);
    ScrollWidget = new QWidget(scrollarea);
    ScrollWidget->setGeometry(0, 0, 300, 40);
    ScrollLayout = new QGridLayout(ScrollWidget);
    scrollarea->setWidget(ScrollWidget);

    //3.3.1-
    Scroll_button.push_back(new QPushButton(tr("1")));
    //Scroll_button[0]->setMinimumSize(40,600);
    Scroll_button[0]->setMaximumSize(40,30);
    ScrollLayout->addWidget(Scroll_button[0],0,0);
    connect(Scroll_button[0],SIGNAL(clicked(bool)),this,SLOT(ScrollSerialSend_slot()));

    //3.3.2-4
    Scroll_lineedit.push_back(new QLineEdit(tr("_&X13;Y5;S50;")));
    Scroll_lineedit[0]->setMinimumSize(120,20);
    ScrollLayout->addWidget(Scroll_lineedit[0],0,1,1,3);

    //3.3.3-
    Scroll_delay_lineedit.push_back(new QLineEdit());
    Scroll_delay_lineedit[0]->setMinimumSize(30,20);
    Scroll_delay_lineedit[0]->setMaximumSize(40,30);
    ScrollLayout->addWidget(Scroll_delay_lineedit[0],0,4);

    //3.3.5-
    Scroll_checkbox.push_back(new QCheckBox());
    //Scroll_checkbox[0]->setMaximumSize(40,30);
    ScrollLayout->addWidget(Scroll_checkbox[0],0,5);
    Scroll_checkbox[0]->setChecked(true);

    //3.4、Scroll下方控件
    ContinSendButton = new QPushButton(codec->toUnicode("连续发送"));
    ContinSendButton->setEnabled(false);
    connect(ContinSendButton,SIGNAL(clicked(bool)),this,SLOT(ContinSendButtonFun()));

    StopContinSendButton = new QPushButton(codec->toUnicode("终止连续发送"));
    //StopContinSendButton->setEnabled(true);
    connect(StopContinSendButton,SIGNAL(clicked(bool)),this,SLOT(StopContinSendFun()));

    AddScrComButton = new QPushButton(codec->toUnicode("增加指令条数"));
    connect(AddScrComButton,SIGNAL(clicked(bool)),this,SLOT(AddScrCom()));

    ContinSend_Precise_Button = new QPushButton(codec->toUnicode("带校核连续发送"));
    ContinSend_Precise_Button->setEnabled(false);
    connect(ContinSend_Precise_Button,SIGNAL(clicked(bool)),this,SLOT(Precise_ContinSendFun()));

    ContinSend_CheckBox = new QCheckBox();
    ContinSend_CheckBox->setEnabled(false);

    ContinSend_Precise_CheckBox = new QCheckBox();
    ContinSend_Precise_CheckBox->setEnabled(false);

    ChatStatu_Label = new QLabel(tr("FreeStatu"));

    //3.4-搜索控件
    //open carmera
    Open_Close_Carmera = new QPushButton(tr("open carmera"));
//    connect(Open_Close_Carmera,SIGNAL(clicked(bool)),this,SLOT(Open_Close_Carmera_Slot_Fun()));

    //search connect
    SearchCupButton = new QPushButton(tr("SearchCup"));
    SearchCupButton->setEnabled(false);
//    connect(SearchCupButton,SIGNAL(clicked(bool)),this,SLOT(SearchCup_Fun()));
    SearchCupLabel = new QLabel(tr("0"));

    //AUTO run
    AutoRunButton = new QPushButton(tr("AutoRun"));
    AutoRunButton->setEnabled(false);
//    connect(AutoRunButton,SIGNAL(clicked(bool)),this,SLOT(AutoRun_Slot()));
    AutoRunCheckBox = new QCheckBox();
    AutoRunCheckBox->setEnabled(false);
    AutoRunLabel = new QLabel(tr("0"));

    //search apart
    SearchCup_Dect_Button = new QPushButton(tr("Dect"));
    SearchCup_Dect_Button->setEnabled(false);
//    connect(SearchCup_Dect_Button,SIGNAL(clicked(bool)),this,SLOT(SearchCup_Dect_Fun()));
    SearchCup_Dis_Button = new QPushButton(tr("Distance"));
    SearchCup_Dis_Button->setEnabled(false);
//    connect(SearchCup_Dis_Button,SIGNAL(clicked(bool)),this,SLOT(SearchCup_Dis_Fun()));

    //4、layout
    layout_grid = new QGridLayout();
    QGridLayout *layout_serial = new QGridLayout();
    layout_grid2 = new QGridLayout();
    layout_grid3 = new QGridLayout();
    layout_main = new QVBoxLayout();

    layout_serial->addWidget(SendText,1,0,1,3);
    layout_serial->addWidget(ComboBox,2,0,1,3);
    layout_serial->addWidget(ScanButton,3,0,1,1);
    layout_serial->addWidget(OpenButton,3,1,1,1);
    layout_serial->addWidget(SendButton,3,2,1,1);

    for(unsigned int i=0;i<=ArmValLab_label.size()-1;i++){
        layout_grid2->addWidget(ArmValLab_label[i],i,0);
        layout_grid2->addWidget(ArmVal_label[i],i,1);
    }

    layout_grid->addWidget(RecvText,0,0,1,4);
    layout_grid->addLayout(layout_serial,1,0,1,2);
    layout_grid->addLayout(layout_grid2,1,2,1,2);
    layout_grid->addWidget(scrollarea,0,4,1,4);
    layout_grid->addLayout(layout_grid3,1,4,1,4);

    //text and send label/checkbox
    layout_grid3->addWidget(new QLabel(codec->toUnicode("连续发送状态:")),0,0);
    layout_grid3->addWidget(ContinSend_CheckBox,0,1);
    layout_grid3->addWidget(new QLabel(codec->toUnicode("带校核连续发送状态:")),1,0);
    layout_grid3->addWidget(ContinSend_Precise_CheckBox,1,1);
    layout_grid3->addWidget(new QLabel(codec->toUnicode("指令阶段状态:")),2,0);
    layout_grid3->addWidget(ChatStatu_Label,2,1);

    //text and send button
    layout_grid3->addWidget(AddScrComButton,0,2);
    layout_grid3->addWidget(StopContinSendButton,0,3);
    layout_grid3->addWidget(ContinSendButton,1,2);
    layout_grid3->addWidget(ContinSend_Precise_Button,1,3);

    //search connect
    layout_grid3->addWidget(new QLabel(tr("Cup Locate:")),3,0);
    layout_grid3->addWidget(SearchCupLabel,3,1);
    layout_grid3->addWidget(SearchCupButton,2,2);

    layout_grid3->addWidget(Open_Close_Carmera,2,3);

    //search apart
    layout_grid3->addWidget(SearchCup_Dect_Button,3,2);
    layout_grid3->addWidget(SearchCup_Dis_Button,3,3);

    //AUTO run
    layout_grid3->addWidget(new QLabel(tr("AutoRun:")),4,0);
    layout_grid3->addWidget(AutoRunLabel,4,1);
    layout_grid3->addWidget(AutoRunCheckBox,4,2);
    layout_grid3->addWidget(AutoRunButton,4,3);

    layout_main->addLayout(layout_grid);
//    layout_main->addLayout(layout_grid2);
//    layout_main->addWidget(scrollarea);
//    layout_main->addLayout(layout_grid3);

    this->setLayout(layout_main);
    this->setWindowTitle(codec->toUnicode("TARM机械臂控制客户端"));

    scanport_slot();

    AddScrComString("_&R13;S10000;");
    AddScrComString("_&C800;");
    AddScrComString("_&C1300;");
    AddScrComString("_&C0;");
    AddScrComString("_&X20;Y-5;S150;");
    AddScrComString("_&C1020;");
    AddScrComString("_&C0;");
    AddScrComString("_&X13;Y5;S150;");
    AddScrComString("_&X25;Y10;S150;");
    AddScrComString("_&X13;Y10;S150;");
    AddScrComString("_&X20;Y-5;S150;");
    AddScrComString("_&X20;Y-5;S150;");
    AddScrComString("_&C1300;");
    AddScrComString("_&C0;");
    AddScrComString("_&X13;Y10;S150;");
    AddScrComString("_&R-13;S10000;");
}

Widget::~Widget()
{
//    if(MainSerial != NULL)
//        MainSerial->close();
    delete ui;
}

// 扫描串口按钮回调函数
void Widget::scanport_slot()
{
    /* 新建串口类 */
    MainSerial = new QSerialPort();
    ComboBox->clear();
    if(MainSerial != NULL)
    {
        /* 查找可用串口 */
        foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
        {
            QSerialPort serial;
            serial.setPort(info);
            /* 判断端口是否能打开 */
            if(serial.open(QIODevice::ReadWrite))
            {
                int isHaveItemInList = 0;
                /* 判断是不是已经在列表中了 */
                for(int i=0; i<ComboBox->count(); i++)
                {
                    /* 如果，已经在列表中了，那么不添加这一项了就 */
                    if(ComboBox->itemData(i) == serial.portName())
                    {
                        isHaveItemInList++;
                    }
                }

                if(isHaveItemInList == 0)
                {
                    ComboBox->addItem(serial.portName());
                }
                serial.close();
            }
        }
    }
    else
    {
        // 串口没有初始化成功，先不处理异常
        // 警告对话框
        QMessageBox::warning(this,tr("警告"),
                             tr("串口没有初始化成功"),QMessageBox::Ok);
    }
}

//打开串口按钮回调函数
void Widget::openport_slot()
{
    /* 先来判断对象是不是为空 */
    if(MainSerial == NULL)
    {
        /* 新建串口对象 */
        MainSerial = new QSerialPort();
    }
    /* 判断是要打开串口，还是关闭串口 */
    if(MainSerial->isOpen())
    {
        /* 串口已经打开，现在来关闭串口 */
        MainSerial->close();
        RecvText->setPlainText(codec->toUnicode("串口在关闭状态"));
        OpenButton->setText(tr("open"));
        ContinSendButton->setEnabled(false);
        ContinSend_Precise_Button->setEnabled(false);
    }
    else
    {
        /* 判断是否有可用串口 */
        if(ComboBox->count() != 0)
        {
            /* 串口已经关闭，现在来打开串口 */
            /* 设置串口名称 */
            MainSerial->setPortName(ComboBox->currentText());
            /* 设置波特率 */
            MainSerial->setBaudRate(MainSerialBaudRate);
            /* 设置数据位数 */
            MainSerial->setDataBits(QSerialPort::Data8);
            /* 设置奇偶校验 */
            MainSerial->setParity(QSerialPort::NoParity);
            /* 设置停止位 */
            MainSerial->setStopBits(QSerialPort::OneStop);
            /* 设置流控制 */
            MainSerial->setFlowControl(QSerialPort::NoFlowControl);
            /* 打开串口 */
            MainSerial->open(QIODevice::ReadWrite);
            /* 设置串口缓冲区大小，这里必须设置为这么大 */
            MainSerial->setReadBufferSize(MainSerialPortOneFrameSize);
            /* 注册回调函数 */
            QObject::connect(MainSerial, &QSerialPort::readyRead, this, &Widget::MainSerialRecvMsgEvent);

            qDebug()<<codec->toUnicode("串口在关闭状态，现在打开了") + ComboBox->currentText();

            qDebug()<<tr("show();");

            RecvText->setPlainText(codec->toUnicode("串口在打开状态"));
            OpenButton->setText(tr("CLOSS"));
            ContinSendButton->setEnabled(true);
            ContinSend_Precise_Button->setEnabled(true);
        }
        else
        {
            qDebug()<<"没有可用串口，请重新常识扫描串口";
            // 警告对话框
            QMessageBox::warning(this,tr("警告"),tr("没有可用串口，请重新尝试扫描串口！"),QMessageBox::Ok);
        }
    }
}

// 串口接收数据事件回调函数
void Widget::MainSerialRecvMsgEvent()
{
    // 如果本次数据帧接受错误，那么先不接受
    if(MainSerial->bytesAvailable() >= MainSerialPortOneFrameSize)
    {
        QByteArray RecvBuf = MainSerial->readAll();
        //MainSerialRecvData = MainSerial->readAll();
        qDebug() << codec->toUnicode("一级缓存满，一级缓存内容：") << RecvBuf;
        if(!RecvBuf.isEmpty())
        {
            MainSerialPortRecvFrameNumber++;
            MainSerialRecvData = MainSerialRecvData + RecvBuf;
        }
        else
        {
            MainSerialPortRecvErrorFrameNumber++;
            qDebug()<<codec->toUnicode("接受数据出错");
        }
    }
    else
    {
        //qDebug()<<codec->toUnicode("一级缓存未满，长度：") + QString::number(MainSerial->bytesAvailable());
        MainSerialRecvData = MainSerialRecvData + MainSerial->readAll();
        //qDebug() << codec->toUnicode("二级缓存内容：") << MainSerialRecvData;

        // 延时，在RecFinish_slot函数里把接受到的数据显示到界面上
        if(!MainSerialRecvData.isEmpty()){
            MainSerialPortRecvFrameNumber = 0;
            //RecvText->setPlainText(readComData);
            //RecvText->appendPlainText(MainSerialRecvData);

            SerialDelayTimer->stop();
            SerialDelayTimer->singleShot(200,this,SLOT(RecFinish_slot()));
        }
        else{
            MainSerialPortRecvErrorFrameNumber++;
            qDebug()<<codec->toUnicode("接受数据出错");
        }
    }

}

void Widget::MainSerialSendMsgEvent()
{
    MainSerial->write(SendText->text().toUtf8()+"\r\n");
}

qint64 Widget::MainSerialSendMsgEvent(char* qbarr,qint64 len)
{
    QByteArray byte;
    byte = QByteArray(qbarr);
    byte = byte + "\r\n";

    return MainSerial->write(byte);
}

void Widget::RecFinish_slot()
{
    if(MainSerialRecvData.isEmpty()==false)
    {
        RecvText->appendPlainText(codec->toUnicode(MainSerialRecvData));
        Handle_RecMsg(QString(MainSerialRecvData));
    }
    MainSerialRecvData.clear();     //清空二级缓存
}


/** ********分界线*****************上下隔绝**************
* 接口：只在上面接收函数加了一行Handle_RecMsg(QString(MainSerialRecvData)); *
************ **/
void Widget::ScrollSerialSend_slot()
{
    if(MainSerial->isOpen())
    {
        QPushButton* button = qobject_cast<QPushButton*>(sender());
        QString button_text = button->text();
        char sendMsgChar[1024] = {0};

        QString *send_str;

        int button_index = button_text.toInt();
        send_str = new QString(Scroll_lineedit[button_index-1]->text());

        strcpy(sendMsgChar, send_str->toStdString().c_str());
        if(MainSerialSendMsgEvent(sendMsgChar, strlen(sendMsgChar)))
            qDebug() << "send success";
        else
            qDebug() << "send error";
    }
    else
    {
        qDebug() << "no connect";
    }
}

//增加框
void Widget::AddScrCom()
{
    ScrollWidget->resize(300,Scroll_lineedit.size()*30+40);

    //
    Scroll_button.push_back(new QPushButton(tr("%1").arg(Scroll_button.size()+1)));
    //Scroll_button[Scroll_button.size()-1]->setMinimumSize(40,600);
    Scroll_button[Scroll_button.size()-1]->setMaximumSize(40,30);
    ScrollLayout->addWidget(Scroll_button[Scroll_button.size()-1],Scroll_button.size()-1,0);
    connect(Scroll_button[Scroll_button.size()-1],SIGNAL(clicked(bool)),this,SLOT(ScrollSerialSend_slot()));

    //
    Scroll_lineedit.push_back(new QLineEdit(tr("_&U1;S50;")));
    Scroll_lineedit[Scroll_lineedit.size()-1]->setMinimumSize(120,20);
    ScrollLayout->addWidget(Scroll_lineedit[Scroll_lineedit.size()-1], Scroll_lineedit.size()-1, 1, 1, 3);

    //
    Scroll_delay_lineedit.push_back(new QLineEdit());
    Scroll_delay_lineedit[Scroll_delay_lineedit.size()-1]->setMinimumSize(30,20);
    Scroll_delay_lineedit[Scroll_delay_lineedit.size()-1]->setMaximumSize(40,30);
    ScrollLayout->addWidget(Scroll_delay_lineedit[Scroll_delay_lineedit.size()-1], Scroll_delay_lineedit.size()-1, 4);

    //3.2.5-
    Scroll_checkbox.push_back(new QCheckBox());
    ScrollLayout->addWidget(Scroll_checkbox[Scroll_checkbox.size()-1],Scroll_checkbox.size()-1,5);
    Scroll_checkbox[Scroll_checkbox.size()-1]->setChecked(true);
}

void Widget::AddScrComString(QString i){
    ScrollWidget->resize(300,Scroll_lineedit.size()*30+40);

    //
    Scroll_button.push_back(new QPushButton(tr("%1").arg(Scroll_button.size()+1)));
    //Scroll_button[Scroll_button.size()-1]->setMinimumSize(40,600);
    Scroll_button[Scroll_button.size()-1]->setMaximumSize(40,30);
    ScrollLayout->addWidget(Scroll_button[Scroll_button.size()-1],Scroll_button.size()-1,0);
    connect(Scroll_button[Scroll_button.size()-1],SIGNAL(clicked(bool)),this,SLOT(ScrollSerialSend_slot()));

    //
    Scroll_lineedit.push_back(new QLineEdit(i));
    Scroll_lineedit[Scroll_lineedit.size()-1]->setMinimumSize(120,20);
    ScrollLayout->addWidget(Scroll_lineedit[Scroll_lineedit.size()-1], Scroll_lineedit.size()-1, 1, 1, 3);

    //
    Scroll_delay_lineedit.push_back(new QLineEdit());
    Scroll_delay_lineedit[Scroll_delay_lineedit.size()-1]->setMinimumSize(30,20);
    Scroll_delay_lineedit[Scroll_delay_lineedit.size()-1]->setMaximumSize(40,30);
    ScrollLayout->addWidget(Scroll_delay_lineedit[Scroll_delay_lineedit.size()-1], Scroll_delay_lineedit.size()-1, 4);

    //3.2.5-
    Scroll_checkbox.push_back(new QCheckBox());
    ScrollLayout->addWidget(Scroll_checkbox[Scroll_checkbox.size()-1],Scroll_checkbox.size()-1,5);
    Scroll_checkbox[Scroll_checkbox.size()-1]->setChecked(true);
}

void Widget::ContinSendButtonFun()
{
    ContinSend_CheckBox->setChecked(true);
    AutoContinSend();
}

void Widget::Precise_ContinSendFun()
{
    ContinSend_Precise_CheckBox->setChecked(true);

    qDebug() << "Precise_ContinSendFun";
    AutoContinSend_Precise();
}

void Widget::StopContinSendFun()
{
    ContinSend_Precise_CheckBox->setChecked(false);
    Precise_ContinIndex = 0;

    ContinSend_CheckBox->setChecked(false);
    ContinIndex = 0;

    RepeatFlag = false;     //清除重复标志
}

void Widget::DelayTimer_Fun()
{
    if(ContinSend_Precise_CheckBox->isChecked()){
        AutoContinSend_Precise();
        qDebug() << "time arrive";
    }
    else if(ContinSend_CheckBox->isChecked()){
        AutoContinSend();
        qDebug() << "time arrive";
    }
}

void Widget::OverTimer_Fun()
{
    if(ContinSend_Precise_CheckBox->isChecked()){
//        OverTimer->singleShot(10000,this,SLOT(OverTimer_Fun()));
        MainSerialSendMsgEvent("_&E0;",8);
    }
    else if(ContinSend_CheckBox->isChecked()){
//        OverTimer->singleShot(10000,this,SLOT(OverTimer_Fun()));
        MainSerialSendMsgEvent("_&E0;",8);
    }
    qDebug() << "OVERtime arrive";
}


/*对传入的字符解析
 *
*/
void Widget::Handle_RecMsg(QString str)
{
    double x,y,prime,vice;

    //解析参数
    if(Handle_RecMsgStdToDou(str, QString(tr("_&x:")), x)!=1){    //_&x:100.57;
        //qDebug() << "no x update";
    }
    else{
        ArmVal_label[0]->setText(QString::number(x));
    }

    if(Handle_RecMsgStdToDou(str, QString(tr("_&y:")), y)!=1){
        //qDebug() << "no y update";
    }
    else{
        ArmVal_label[1]->setText(QString::number(y));
    }

    if(Handle_RecMsgStdToDou(str, QString(tr("_&prime:")), prime)!=1){
        //qDebug() << "no prime update";
    }
    else{
        ArmVal_label[2]->setText(QString::number(prime));
    }

    if(Handle_RecMsgStdToDou(str, QString(tr("_&vice:")), vice)!=1){
        //qDebug() << "no vice update";
    }
    else{
        ArmVal_label[3]->setText(QString::number(vice));
    }

    //解析状态
     qDebug() << "\nstr:\n" << str;
    if(str.indexOf(QString(tr("ExecuteFinish")))!=-1){
        ChatStatu = FreeStatu;
        ChatStatu_Label->setText(tr("FreeStatu"));
    }
    else if(str.indexOf(QString(tr("VerifySuccess")))!=-1){
        ChatStatu = ExecuteStatu;
        ChatStatu_Label->setText(tr("ExecuteStatu"));
    }
    else if(str.indexOf(QString(tr("VerifyError")))!=-1){
        ChatStatu = FreeStatu;
        ChatStatu_Label->setText(tr("FreeStatu"));
    }
//    else if(str.indexOf(QString(tr("_&")))!=-1){
//        qDebug() << "REC IGNORE";
//    }
    else
    {
        //MainSerialSendMsgEvent("_&E0;",8);
        qDebug() << "REC ERROR";
    }

    //普通循环发送
    if(ContinSend_CheckBox->isChecked())
    {
        QString delay_time;

        if(Scroll_delay_lineedit[ContinIndex-1]->text().isEmpty()){
            AutoContinSend();
        }
        else{
            delay_time = Scroll_delay_lineedit[ContinIndex-1]->text();
            if(delay_time.toDouble()==0){
                AutoContinSend();
            }
            else{
                DelayTimer->singleShot(delay_time.toDouble(),this,SLOT(DelayTimer_Fun()));
                //DelayTimer->start(delay_time.toDouble());
                qDebug() << "start time";
            }
        }
    }

    //带检校的精确循环发送
    if(ContinSend_Precise_CheckBox->isChecked())
    {
        QString delay_time;

        if(RepeatFlag == true){
            AutoContinSend_Precise();
        }
        else if(Scroll_delay_lineedit[Precise_ContinIndex-1]->text().isEmpty()){
            AutoContinSend_Precise();
        }
        else{
            delay_time = Scroll_delay_lineedit[Precise_ContinIndex-1]->text();
            if(delay_time.toDouble()==0){
                AutoContinSend_Precise();
            }
            else{
                DelayTimer->singleShot(delay_time.toDouble(),this,SLOT(DelayTimer_Fun()));
                //DelayTimer->start(delay_time.toDouble());
                qDebug() << "start time";
            }
        }
    }
}

/*
 *str：整条字符串
 *dis_str：起始符
 *result：返回读取double
*/
int Widget::Handle_RecMsgStdToDou(QString str, QString dis_str, double &result)
{
    if(str.indexOf(dis_str)!=-1){
        int str_locate = str.indexOf(dis_str)+dis_str.length();
        int end_locate = str.indexOf(";",str_locate);
        if(end_locate==-1)
            return -2;
        QString result_str = str.mid(str_locate,end_locate-str_locate);
        qDebug() << "result_str:" << result_str;
        //result = atof(result_str.c_str());

        bool judge_error;
        result = result_str.toDouble(&judge_error);
        if(!judge_error)
            return -3;
        else
            return 1;
    }
    else
        return -1;
}
